<%@page import="com.googlecode.contests_repo.core.users.AccessRight"%>
<%@page import="com.googlecode.contests_repo.core.users.UserManager"%>
<%@page import="java.util.regex.Pattern"%>
<%@page import="java.util.Comparator"%>
<%@page import="java.util.Arrays"%>
<%@page import="java.net.URLDecoder"%>
<%@page import="java.net.URLEncoder"%>
<%@page import="com.googlecode.contests_repo.core.config.ConfigurationManager"%>
<%@page import="java.io.File"%>
<%@page import="com.googlecode.contests_repo.core.model.ResourcesUtils"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<%
	String loggedUser = (String) session.getAttribute("username");

    String resourcePath = request.getParameter("path");
	String repoRoot = ConfigurationManager.getInstance().getRepoRoot();
	String absolutePath = repoRoot + File.separator + resourcePath;
%>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link href="css/default.css" rel="stylesheet" type="text/css">
<title>Browse Folder</title>
</head>
<body>

<h3> <%= resourcePath.replaceAll(Pattern.quote(File.separator), " > ") %>: </h3>
<%    
    File folder = new File(absolutePath);
    File[] files = folder.listFiles();
    if (files != null) {
        
    	Arrays.sort(files, new Comparator<File>() {
       	    public int compare(File f1, File f2) {
        		if (f1.isDirectory() && !f2.isDirectory()) {
        			return -1;
        		}
        		
        		if (f2.isDirectory() && !f1.isDirectory()) {
        			return 1;
        		}
        		
        		return f1.getName().compareTo(f2.getName());
        	}
        });
%>

		<h4> Content: </h4>
		<table border=1>
<%
    	for (File file : files) {
    		String path = resourcePath + File.separator + file.getName();
    		String viewLink = null;
    		if (file.isDirectory()) {
    			viewLink = "resource.jsp?path=" + path;
    		} else {
    			viewLink = "download_file?path=" + path;
    		}
    		String deleteLink = "delete_file?path=" + path;
%>
			<tr> 
				<td width="200px">				
					<a href="<%= viewLink %>"> <%= file.getName() %> </a>
				</td>
<% 
				if (UserManager.hasAccessRight(loggedUser, absolutePath, AccessRight.VIEW_FULL)) {
%>
					<td>
						<a href="<%= deleteLink %>"> delete	</a>
					</td>
<% 
				}
%>
			</tr>
<%
    	}
%>
		</table>
<%
    }
%>

<% 
	if (UserManager.hasAccessRight(loggedUser, absolutePath, AccessRight.VIEW_FULL)) {
%>
<form action="new_folder">
<fieldset>
<legend> New folder</legend>
<label for="folderName"> Name: </label> 
<input type="text" name="folderName"/>
<input type="hidden" name="location" value="<%= resourcePath %>" /> 
<input type="submit" value="Create" />
</fieldset>
</form>

<form method="post" action="upload_file" enctype="multipart/form-data">
<fieldset>
<legend> Upload file </legend>
<input type="hidden" name="location" value="<%= resourcePath %>" /> 
<input type="file" name="file" value="Browse" />
<input type="submit" value="Upload" />
</fieldset>
</form>
<% 
	}

	String parent = folder.getParent();
	parent = parent.substring(repoRoot.length() + 1);
	if (!"_public_files".equals(folder.getName()) && !"_files".equals(folder.getName())) { 
%>
		<a href="resource.jsp?path=<%=parent%>"> Back </a>
<%
	}
%>
</body>
</html>